﻿// <copyright file="ArticlesPage.xaml.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-05-03</date>
// <summary>Article Demo Page</summary>

namespace DemoNYTimes
{
    using System.Windows;
    using System.Windows.Controls;
    using NYTimes;
    using NYTimes.Data;

    /// <summary>
    /// The Articles Demo page
    /// </summary>
    public partial class ArticlesPage : System.Windows.Controls.Page
    {
        /// <summary>
        /// Initializes a new instance of the ArticlesPage class.
        /// </summary>
        public ArticlesPage()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Search for articles
        /// </summary>
        /// <param name="sender">the search button</param>
        /// <param name="e">the routed event arguments</param>
        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            this.DoQuery();
        }

        /// <summary>
        /// Start an article search query
        /// </summary>
        private void DoQuery()
        {
            var articles = this.Resources["Articles"] as Articles;

            var item = this.Query.SelectedItem as Tag;

            if (item == null)
            {
                articles.Query = this.Query.Text;
            }
            else
            {
                articles.Query = item.Query;
            }
        }

        /// <summary>
        /// Image failed event handler
        /// </summary>
        /// <param name="sender">the image that failed to load its image</param>
        /// <param name="e">the exception routed event arguments</param>
        private void OnImageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Image Failed: {0}", e.ErrorException.Message);
        }
    }
}
